package com.xb.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xb.entity.Department;
import com.xb.entity.User;
import com.xb.mapper.DepartmentMapper;
import com.xb.mapper.RegisterMapper;
import com.xb.mapper.UserMapper;
import com.xb.service.IDepartmentService;
import com.xb.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Service业务层处理
 */
@Service
public class DepartmentServiceImp implements IDepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private RegisterMapper registerMapper;


    @Override
    public Result add(Department department) {
        Result result = new Result();
        if (departmentMapper.selectDepartmentByName(department.getName()) != null) {
            result.setMessage("此部门已存在！");
        } else if (departmentMapper.insertDepartment(department) > 0) {
            result.setMessage("添加成功！");
        } else {
            result.setMessage("添加失败！");
        }
        return result;

    }

    /**
     * 分页查询列表
     *
     * @param department
     * @return
     */
    @Override
    public Result selectDepartmentList(Department department, Integer page, Integer limit) {
        Result result = new Result();
        PageHelper.startPage(page, limit);
        List<Department> list = departmentMapper.selectDepartmentList(department);
        PageInfo<Department> departmentPageInfo = new PageInfo<>(list);
        if (departmentPageInfo.getList() != null) {
            result.setData(departmentPageInfo.getList());
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("查询成功！");
            result.setTotal(departmentPageInfo.getTotal());
        }
        return result;
    }

    /**
     * 查询列表
     *
     * @param department
     * @return
     */
    @Override
    public List<Department> selectDepartmentList(Department department) {
        return departmentMapper.selectDepartmentList(department);
    }

    /**
     * 新增
     *
     * @param department
     * @return 结果
     */
    @Override
    public int insertDepartment(Department department) {
        return departmentMapper.insertDepartment(department);
    }

    /**
     * 修改
     *
     * @param department
     * @return 结果
     */
    @Override
    public int updateDepartment(Department department) {
        return departmentMapper.updateDepartment(department);
    }

    /**
     * 删除信息
     *
     * @param id ID
     * @return 结果
     */
    @Override
    public Result deleteDepartmentById(Integer id) {
        Result result = new Result();
        List<User> users = userMapper.selectIncumbentsByDeId(id);
        if (!userMapper.selectIncumbentsByDeId(id).isEmpty()) {
            result.setMessage("此部门还有就职人员，禁止删除！");
            return result;
        }
        if (!registerMapper.selectIncumbentsByDeId(id).isEmpty()) {
            result.setMessage("此部门还有待诊患者，禁止删除！");
            return result;
        }
        if (departmentMapper.deleteDepartmentById(id) > 0) {
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("删除成功！");
        } else {
            result.setMessage("删除失败！");
        }
        return result;

    }
}
